:: 基于 Windows Visual Studio 2015 的 paho mqtt c/cpp 编译脚本
:: 可选参数:
::   /CPP            - 编译paho.mqtt.cpp
::   /C              - 编译paho.mqtt.c
::   /T              - 编译运行单元测试
::   /E              - 编译例子程序
::   /S              - 编译动态库
::   /MT             - 指定/MT编译选项,默认使用/MD(此选项对paho.mqtt.c无效)

@ECHO OFF
SETLOCAL
ECHO make paho mqtt VS2015 project
IF NOT DEFINED VS140COMNTOOLS (
	ECHO vs2015 NOT FOUND.
	EXIT /B -1
)
ECHO vs2015 found.
WHERE cmake
IF ERRORLEVEL 1 (
	ECHO cmake NOT FOUND.
	EXIT /B -1
)
ECHO cmake found
SET sh_folder=%~dp0
:: 删除最后的 '\'
SET sh_folder=%sh_folder:~0,-1%

SET PROJECT_TYPE=c
SET BUILD_SAMPLES=FALSE
SET ENABLE_TEST=OFF
SET rel_suffix=_windows_vc_x86-64
SET with_static_crt=OFF
SET BUILD_SHARED=OFF
:: parse command arguments
:loop
IF x%1 == x GOTO :pare_end
IF /I "%1" == "/CPP"    SET PROJECT_TYPE=cpp
IF /I "%1" == "/C"      SET PROJECT_TYPE=c
IF /I "%1" == "/T"      SET ENABLE_TEST=TRUE
IF /I "%1" == "/E"      SET BUILD_SAMPLES=TRUE
IF /I "%1" == "/MT"     SET with_static_crt=ON && SET rel_suffix=_windows_vc_mt_x86-64

SHIFT
GOTO :loop
:pare_end

SET src_folder=paho.mqtt.%PROJECT_TYPE%
SET release_prefix=paho_mqtt_%PROJECT_TYPE%

SET cmake_prefix_path=
IF "%PROJECT_TYPE%" == "cpp" (
    SET cmake_prefix_path=-DCMAKE_PREFIX_PATH=%sh_folder%\release\paho_mqtt_c%rel_suffix%
)
SET _build_test=-DPAHO_ENABLE_TESTING=%ENABLE_TEST%
IF "%PROJECT_TYPE%" == "cpp" (
    SET _build_test=-DPAHO_BUILD_TESTS=%ENABLE_TEST%
)

SET project_folder=%sh_folder%\build\%release_prefix%.vs2015
SET outdir=%sh_folder%\release\%release_prefix%%rel_suffix%
IF "%OUTDIR%x" == "x" SET OUTDIR=%outdir%

ECHO project_folder=%project_folder%
ECHO OUTDIR=%OUTDIR%

:: 生成项目工程文件
IF EXIST %project_folder% RMDIR %project_folder% /s/q 
MKDIR %project_folder% 
PUSHD %project_folder% || EXIT /B

IF NOT DEFINED VisualStudioVersion (
	ECHO make MSVC environment ...
	CALL "%VS140COMNTOOLS%..\..\vc/vcvarsall" x86_amd64
)

ECHO creating x86_64 Project for Visual Studio 2015 ...
CMAKE %sh_folder%\%src_folder% -G "Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX=%OUTDIR% ^
	-DCMAKE_USER_MAKE_RULES_OVERRIDE=%sh_folder%\cmake\compiler_flag_overrides.cmake ^
    -DPAHO_BUILD_SAMPLES=%BUILD_SAMPLES% ^
	%_build_test% ^
    -DCMAKE_DEBUG_POSTFIX=_d ^
	-DWITH_STATIC_CRT=%with_static_crt% ^
    %cmake_prefix_path% ^
    -DPAHO_BUILD_STATIC=TRUE -DPAHO_BUILD_SHARED=%BUILD_SHARED% -DPAHO_WITH_SSL=FALSE	|| EXIT /B

cmake --build . --config Release --target install -- /maxcpucount || EXIT /B
cmake --build . --config Debug   --target install -- /maxcpucount || EXIT /B
POPD
ENDLOCAL